package area

import (
	"Project/global"
	"Project/model/area/request"
	"Project/model/area/response"
	"Project/model/drugs"
	"Project/model/user"
	"Project/utils"
	"go.uber.org/zap"
	"log"
)
type ShopService struct{}

func (s *ShopService) ShopList(userID int64)(data []response.ShopRep,err error) {
//	 查询用户属于的区县
	ar := user.UserArea{}
	if err = global.GVA_DB.Select("area_id").Find(&ar,"user_id = ?",userID).Error;err != nil {
		log.Println("查询失败",err)
		return nil,err
	}
//	 根据市区ID获取改区县所有药店ID列表
	shopList := []drugs.DrugsShop{}
	if err = global.GVA_DB.Select("drugs_shop_id").Find(&shopList,"area_id = ?",ar.AreaID).Error;err != nil {
		log.Println("获取药店ID列表失败")
		return nil,err
	}
	shopIDList := make([]int64,len(shopList))
	for i,v := range shopList {
		shopIDList[i] = v.DrugsShopID
	}
//	根据药店ID列表查询所有的药店信息
	err = global.GVA_DB.Table("drugs_shop").Select([]string{"shop_id","name","credit_code","register_addr","user_name","phone","card"}).
		Joins("inner join user_sale on drugs_shop.drugs_shop_id = user_sale.shop_id").
		Find(&data,"shop_id IN ? and shop_keeper = ?",shopIDList,true).Error
	if err != nil {
		log.Println("查询药店信息失败")
		return nil,err
	}
//	 返回
	return data,nil
}

// ShopUserList 获取某一药店成员
func (s *ShopService) ShopUserList(shopID int64)(data []response.UserInfoReq,err error){
	sale := []user.UserSale{}
	if err = global.GVA_DB.Select([]string{"user_name","sale_id","phone","card"}).Find(&sale,"shop_id = ?",shopID).Error; err != nil {
		log.Println("查询药店人员信息失败")
		return nil,err
	}
	data = make([]response.UserInfoReq,len(sale))
	for i, v := range sale {
		data[i].UserName = v.UserName
		data[i].Phone = v.Phone
		data[i].Card = v.Card
		data[i].SaleID = v.SaleID
	}
	return data,nil
}
// DelSHop 删除药店信息
func (s *ShopService) DelSHop(shopID int64)(err error) {
	if err = global.GVA_DB.Where("drugs_shop_id = ?",shopID).Delete(&drugs.DrugsShop{}).Error;err != nil {
		log.Println("删除失败")
		global.GVA_LOG.Info("药店表删除失败",zap.Error(err))
		return
	}
	// 需要定期清除员工表、登记表表里面的记录信息
	if err = global.GVA_DB.Where("drugs_shop_id = ?",shopID).Delete(&drugs.DrugsRecord{}).Error;err != nil {
		log.Println("删除失败")
		global.GVA_LOG.Info("药店表删除失败",zap.Error(err))
		return
	}
	return
}
// CreateDrugShop 创建药铺
func (s *ShopService) CreateDrugShop(userID int64,drugShop request.DrugShop)(err error) {
		var area user.UserArea
		if err = global.GVA_DB.Select("area_id").Find(&area,"user_id = ?",userID).Error;err != nil {
			return err
		}
		shop := drugs.DrugsShop{
			DrugsShopID: utils.GenID(),
			Name: drugShop.Name,
			AreaID:area.AreaID,
			RegisterAddr: drugShop.RegisterAddr,
			CreditCode: drugShop.CreditCode,
		}
		if err = global.GVA_DB.Create(&shop).Error;err != nil {
			return err
		}
		return nil
}

// CreateShopKeeper 创建店主
func (s *ShopService) CreateShopKeeper(keeper request.DrugKeeper)(err error)  {
	sale := user.UserSale{
		SaleID: utils.GenID(),
		ShopID: keeper.ShopID,
		ShopKeeper: true,
		UserCommon:global.UserCommon{
			UserName: keeper.UserName,
			AuthID: global.ShopAuth,
			Card: keeper.Card,
			Phone: keeper.Phone,
			Password: utils.BcryptHash(keeper.Password),
			IsUse: keeper.IsUse,
		},
	}
	if err = global.GVA_DB.Create(&sale).Error;err != nil{
		return err
	}
	return nil
}
// EditorShop 修改店铺信息
func (s *ShopService) EditorShop(shopRes request.ShopRes)(err error) {
//	 更新商店信息
	shop := drugs.DrugsShop{}
	if err = global.GVA_DB.Model(&shop).Where("drugs_shop_id = ?",shopRes.ShopID).Updates(map[string]interface{}{"name":shopRes.ShopName,"register_addr":shopRes.RegisterAddr,"credit_code":shopRes.CreditCode}).Error; err != nil{
		log.Println("更新失败",err)
		return
	}
//  获取旧的店主信息
	oldKeeper,err := s.QueryKeeperByID(shopRes.ShopID)
	if err != nil{
		log.Println("查询失败",err)
		return
	}
//  没有修改店主信息，直接返回
	if oldKeeper.SaleID == shopRes.SaleID && oldKeeper.UserName == shopRes.UserName && oldKeeper.Phone == shopRes.UserPhone && oldKeeper.Card == shopRes.UserCard{
		return nil
	}
//   如果修改了当前店主的信息
	if oldKeeper.SaleID == shopRes.SaleID{
		err = global.GVA_DB.Model(&user.UserSale{}).Where("shop_id = ? and sale_id = ?",oldKeeper.ShopID,oldKeeper.SaleID).
			Updates(map[string]interface{}{"user_name":shopRes.UserName,"phone":shopRes.UserPhone,"card":shopRes.UserCard}).Error
		return err
	}
//	 修改了负责人的信息

// 旧的负责人身份修改为false
	if err = global.GVA_DB.Model(&user.UserSale{}).Where("shop_id = ? and shop_keeper = ?",shopRes.ShopID,true).Update("shop_keeper",false).Error;err != nil{
		log.Println("更新失败",err)
		return
	}
// 更新新的负责人
  if err = global.GVA_DB.Model(&user.UserSale{}).Where("shop_id = ? and sale_id = ?",shopRes.ShopID,shopRes.SaleID).
	  Updates(map[string]interface{}{"phone":shopRes.UserPhone,"card":shopRes.UserCard,"shop_keeper":true}).Error;err != nil {
	  log.Println("更新失败",err)
	  return
  }
	return nil
}



// QueryShopByID 查询商店信息
func (s *ShopService) QueryShopByID(shopID int64)(shop drugs.DrugsShop,err error) {
	if err = global.GVA_DB.Find(&shop,"shop_id = ?",shopID).Error;err != nil{
		return
	}
	return
}

// QueryKeeperByID 查询负责人信息
func (s *ShopService) QueryKeeperByID(shopID int64)(shopKeeper user.UserSale,err error)  {
	if err = global.GVA_DB.Find(&shopKeeper,"shop_id = ? and shop_keeper = ?",shopID,true).Error; err != nil{
		return
	}
	return
}





